package com.woniuxy.controller;

import com.woniuxy.anno.BloomFilterAnnotation;
import com.woniuxy.anno.PreAuthorize;
import com.woniuxy.entity.PowerUser;
import com.woniuxy.entity.dto.PowerUserDTO;
import com.woniuxy.service.PowerUserService;
import com.woniuxy.util.MinioUtils;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: tsm
 * @description:
 * @create: 2024-10-16 21:15
 **/
@RestController
@RequestMapping("/powerUser")
public class PowerUserController {
    @Autowired
    private PowerUserService powerUserService;
    @Autowired
    private MinioUtils minioUtils;

    @PostMapping("/login")
    public ResponseData<?> login(@RequestBody PowerUser powerUser) {
        try {
            return powerUserService.login(powerUser);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/signOut")
    public ResponseData<?> signOut(@RequestBody PowerUser powerUser) {
        return powerUserService.SignOut(powerUser);
    }

    @PostMapping("/resetPassword")
    public ResponseData<?> resetPassword(@RequestBody PowerUser powerUser){
        return powerUserService.resetPassword(powerUser.getUsername(),"1");
    }

    @GetMapping("/queryById/{id}")
    public ResponseData<?> queryById(@PathVariable("id") Integer id){
        return powerUserService.queryById(id);
    }

    @PostMapping("/updatePerson")
    public ResponseData<?> updatePerson(@RequestBody PowerUser powerUser){
        return powerUserService.updatePerson(powerUser);
    }

    @PostMapping("/updateImg")
    public ResponseData<?> updateImg(@RequestBody PowerUser powerUser){
        return powerUserService.updateImg(powerUser.getId(),powerUser.getImgSrc());
    }

    /**
     * 头像上传解决
     * 怎么接收（图片）文件类型数据
     * MultipartFile:接收文件类型数据
     */
    @PostMapping("/imgLoad")
    public ResponseData<?> uploadImg(MultipartFile file){
        try {
            return minioUtils.uploadFile(file, "img");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败");
        }
    }

    @PostMapping("/queryAllPowerUser")
    public ResponseData<?> queryAllPowerUser(@RequestBody PageData<PowerUserDTO> pageData){
        return powerUserService.queryAllPowerUser(pageData);
    }

    @GetMapping("/updateStatus/{id}")
    @PreAuthorize(value = "kaichu")
    public ResponseData<?> updateStatus(@PathVariable("id") Integer id){
        return powerUserService.updateStatus(id);
    }

    @PostMapping("/addPowerUser")
    public ResponseData<?> addPowerUser(@RequestBody PowerUserDTO powerUserDTO){
        return powerUserService.addPowerUser(powerUserDTO);
    }


    @GetMapping("/queryPower/{id}")
    public ResponseData<?> queryPower(@PathVariable("id") Integer id){
        return powerUserService.queryPower(id);
    }

    @PostMapping("/updatePower")
    public ResponseData<?> updatePower(@RequestBody PowerUserDTO powerUserDTO){
        return powerUserService.updatePower(powerUserDTO.getRoleId(),powerUserDTO.getPoweruserId());
    }
}